//
//  NSString+Extension.m
//  QMUI_TEST
//
//  Created by mac on 17/4/28.
//  Copyright © 2017年 Apple. All rights reserved.
//

#import "NSString+Extension.h"

@implementation NSString (Extension)

/*!
 *  时间戳格式化
 *  Formatting  时间戳字符串
 *  Style       yyyy-MM-dd HH:MM:ss or yyyy.mm.dd HH:mm:ss
 *  return      格式化成功后的字符串 2017-04-28 11:13:00
 */
+(NSString *)timestampFormatting:(NSString *)timestamp FormattingStyle:(NSString *) style{
    if (!timestamp || !style) {
        return nil;
    }
//    NSDateFormatter* formatter = [[NSDateFormatter alloc] init];
//    [formatter setDateFormat:style];
//    NSDate *confromTimesp = [NSDate dateWithTimeIntervalSince1970:[timestamp integerValue]];
//    NSString *nowStr = [formatter stringFromDate:confromTimesp];
//    return nowStr;
    NSDate *confromTimesp = [NSDate dateWithTimeIntervalSince1970:[timestamp doubleValue]];
    
    //实例化一个NSDateFormatter对象
    
    NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
    
    //设定时间格式,这里可以设置成自己需要的格式
    
    [formatter setDateFormat:style];
    
    NSString *currentDateStr = [formatter stringFromDate: confromTimesp];
    return currentDateStr;
}
/*!
 *  时间转时间戳
 *  @currentTimer 需要格式化的时间
 *  @format       格式 yyyy-MM-dd HH:MM:ss or yyyy.mm.dd HH:mm:ss
 */
+(NSString *) changeEnterTimer:(NSString *)currentTimer format:(NSString *)format{
    NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
    [formatter setDateStyle:NSDateFormatterMediumStyle];
    [formatter setTimeStyle:NSDateFormatterShortStyle];
    [formatter setDateFormat:format]; //设置你想要的格式,hh与HH的区别:分别表示12小时制,24小时制
    NSTimeZone* timeZone = [NSTimeZone timeZoneWithName:@"Asia/Shanghai"];
    [formatter setTimeZone:timeZone];
    
    NSDate* date = [formatter dateFromString:currentTimer]; //------------将字符串按formatter转成nsdate
    return [NSString stringWithFormat:@"%ld", (long)[date timeIntervalSince1970]];

}
/*!
 *  中文编码处理
 */
- (NSString *)unicodeString{
    
    NSString *tempStr1 = [self stringByReplacingOccurrencesOfString:@"\\u" withString:@"\\U"];
    
    NSString *tempStr2 = [tempStr1 stringByReplacingOccurrencesOfString:@"\"" withString:@"\\\""];
    
    NSString *tempStr3 = [[@"\"" stringByAppendingString:tempStr2] stringByAppendingString:@"\""];
    
    NSData *tempData = [tempStr3 dataUsingEncoding:NSUTF8StringEncoding];
    
    
    NSPropertyListFormat format = NSPropertyListOpenStepFormat;
    
    NSString *returnStr = [NSPropertyListSerialization propertyListWithData:tempData options:NSPropertyListImmutable format:&format error:nil];
    
    
    
    return [returnStr stringByReplacingOccurrencesOfString:@"\\r\\n" withString:@"\n"];
    
    
}

+(NSString *)timerForamting:(NSInteger )count{
    NSInteger min = count / 60;
    NSInteger s   = count % 60;
    return [NSString stringWithFormat:@"%zd:%.2ld", min, s];
}

/**
 * 获取距离现在多少年的时间戳
 */
+(NSString *)distanceCurrenDateYear:(NSInteger)year{
    NSString *timestr = nil;
    NSCalendar *calendar = [NSCalendar currentCalendar];
    NSInteger nowYear = [calendar component:NSCalendarUnitYear fromDate:[NSDate date]];
    NSInteger nowMonth = [calendar component:NSCalendarUnitMonth fromDate:[NSDate date]];
    NSInteger nowDay = [calendar component:NSCalendarUnitDay fromDate:[NSDate date]];
    NSInteger nowHour = [calendar component:NSCalendarUnitHour fromDate:[NSDate date]];
    NSInteger nowM = [calendar component:NSCalendarUnitMinute fromDate:[NSDate date]];
    NSInteger nowS = [calendar component:NSCalendarUnitSecond fromDate:[NSDate date]];
    timestr = [NSString stringWithFormat:@"%zd-%02ld-%02ld %zd:%zd:%zd", nowYear + year, (long)nowMonth, (long)nowDay, nowHour, nowM, nowS];
    return [self changeEnterTimer:timestr format:@"yyyy-MM-dd HH:mm:ss"];
}

/**
 获取距离从指定时间到多少年后的时间戳

 @param time 开始时间戳
 @param year 距离年份

 @return 指定时间某年后的时间戳
 */
+(NSString *)formTimeStr:(NSString *)time distanceDateYear:(NSInteger)year{
    NSString *timeFormater = [NSString timestampFormatting:time FormattingStyle:@"yyyy-MM-dd HH:mm:ss"];
    NSString *yearStr = [timeFormater substringWithRange:NSMakeRange(0, 4)];
    NSString *newYer = [NSString stringWithFormat:@"%zd",[yearStr integerValue] + year];
    timeFormater = [timeFormater stringByReplacingOccurrencesOfString:yearStr withString:newYer];
    
    return [self changeEnterTimer:timeFormater format:@"yyyy-MM-dd HH:mm:ss"];;
}

/** 文本附件（图片）*/
-(NSMutableAttributedString *)settingAttachmentImage:(UIImage *)image frame:(CGRect) frame{
    if (!image) {
        return nil;
    }
    NSTextAttachment *attach = [NSTextAttachment new];
    attach.image = image;
    attach.bounds = frame;
    NSAttributedString *imageIcon = [NSAttributedString attributedStringWithAttachment:attach];
    NSMutableAttributedString *attributedImageText = [[NSMutableAttributedString alloc] initWithAttributedString:imageIcon];
    
    NSMutableAttributedString *attributedStr = [[NSMutableAttributedString alloc] initWithString:self];
    [attributedImageText appendAttributedString:attributedStr];
    return attributedImageText;
}


-(NSMutableAttributedString *)settingAttachmentImage:(UIImage *)image frame:(CGRect) frame fontColor:(UIColor *)fontColor range:(NSRange)range{
    
    if (!image) {
        return nil;
    }
    NSTextAttachment *attach = [NSTextAttachment new];
    attach.image = image;
    attach.bounds = frame;
    NSAttributedString *imageIcon = [NSAttributedString attributedStringWithAttachment:attach];
    NSMutableAttributedString *attributedImageText = [[NSMutableAttributedString alloc] initWithAttributedString:imageIcon];
    
    NSMutableAttributedString *attributedStr = [[NSMutableAttributedString alloc] initWithString:self];
    if (fontColor) {
        [attributedStr addAttribute:NSForegroundColorAttributeName value:fontColor range:range];
    }
    [attributedImageText appendAttributedString:attributedStr];
    
    return attributedImageText;
}

-(NSMutableAttributedString *)settingAttachmentImage:(UIImage *)image frame:(CGRect) frame attributedParmas:(NSDictionary *)parmase{
    if (!image) {
        return nil;
    }
    NSTextAttachment *attach = [NSTextAttachment new];
    attach.image = image;
    attach.bounds = frame;
    NSAttributedString *imageIcon = [NSAttributedString attributedStringWithAttachment:attach];
    NSMutableAttributedString *attributedImageText = [[NSMutableAttributedString alloc] initWithAttributedString:imageIcon];
    
    NSMutableAttributedString *attributedStr = [[NSMutableAttributedString alloc] initWithString:self];
    [attributedStr addAttributes:parmase range:NSMakeRange(0, self.length)];
    [attributedImageText appendAttributedString:attributedStr];
    return attributedImageText;
}
+ (BOOL) isEmpty:(NSString *) str {
    
    if(!str) {
        
        return true;
        
    }else {
        
        
        //A character set containing only the whitespace characters space (U+0020) and tab (U+0009) and the newline and next line characters (U+000A–U+000D,U+0085).
        
        
        NSCharacterSet *set = [NSCharacterSet whitespaceAndNewlineCharacterSet];
        
        
        //Returns a new string made by removing from both ends of the receiver characters contained in a given character set.
        
        
        NSString *trimedString = [str stringByTrimmingCharactersInSet:set];
        
        
        if([trimedString length] == 0) {
            
            return true;
            
            
        }else {
            
            
            return false;
            
            
        }
        
    }
    
    
}
-(js_subTime)gainTime{
    return ^NSInteger(js_subTimeStyle style){
        NSString *str = [NSString timestampFormatting:self FormattingStyle:@"yyyy-MM-dd"];
        NSArray *strAr = [str componentsSeparatedByString:@"-"];
        if (style == kSubTimeStyle_Year) {
            NSString *subStr = strAr.firstObject;
            return subStr.integerValue;
        }else if (style == kSubTimeStyle_Month){
            NSString *subStr = strAr[1];
            return subStr.integerValue;
        }else{
            NSString *subStr = strAr.lastObject;
            return subStr.integerValue;
        }
        return 0;
    };
}

+(NSMutableAttributedString *)seetingFormString:(NSString *)formString subString:(NSString *)subString defauldColor:(UIColor *)defauldColor subColor:(UIColor *)subColor{
    
    NSMutableAttributedString *attributedString = [[NSMutableAttributedString alloc] initWithString:formString attributes:@{NSForegroundColorAttributeName: defauldColor}];
    NSRange range = [formString rangeOfString:subString];
    if (range.location != NSNotFound) {
        [attributedString addAttributes:@{NSForegroundColorAttributeName: subColor} range:range];
    }
    
    return attributedString;
}
@end
